import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;

public class GenerateDirectory {    

    private static String input = new String();
    private static Vector vInput= new Vector(10);
    private static Vector vOut = new Vector(10);

    public static void main(String[] args) {
	if (args.length != 2) {
	    System.out.println("[Get directory for ....]");
	    System.out.println("[category name][DISPLAY/MAKE]");
	    System.exit(0);
	}

	String category = args[0];
	GenerateDirectory myConnecter = new GenerateDirectory();
	String url = "http://directory.google.com/Top/"+category;
	myConnecter.connectToUrl(url);
	vInput = myConnecter.putInVectorByLine(input);
	vOut = myConnecter.selectUsefulInfo(vInput);
	myConnecter.displayVector(vOut);

	if (args[1].equals("MAKE")) {
	    myConnecter.makeDirectory(("c:/projects/apache2/htdocs/"+category));
	    Enumeration enum = vOut.elements();
	    while ( enum.hasMoreElements() ) {
		String temp = (String)enum.nextElement();
		myConnecter.makeDirectory(("c:/projects/apache2/htdocs/"+category+"/"+temp));
	    }
	} 
    }

    private Vector putInVectorByLine (String input) {

	Vector v = new Vector (10);
	String element = "";
	StringTokenizer tokens = new StringTokenizer(input, "\n");
	while (tokens.hasMoreTokens()) {
	    element = tokens.nextToken();
	    if (element != null) {
		v.addElement(element);
		//System.out.println(element);
	    }
	}
	
	return v;
    }

    private Vector selectUsefulInfo (Vector v_in) {
	Vector v_out = new Vector (10);
	String text = "";
	Enumeration penum = v_in.elements();
	while ( penum.hasMoreElements()) {

	    text = (String)penum.nextElement();
	    if (text.startsWith("Categories")) {
		break;
	    }
	}

	while ( penum.hasMoreElements() ) {
	    text = (String)penum.nextElement();
	    if ((text.trim()).equals("Related Category:")||((text.trim()).startsWith("Help build")) ||
		(text.trim()).startsWith("Related Category")) {
		break;
	    }

	    if (!(text.trim()).startsWith("<") && !(text.trim()).startsWith("(")&& !(text.trim()).startsWith("-http")) {
		v_out.addElement(text);
	    }
	}

	return v_out;
    }

    private void displayVector( Vector inputV) {
	String temp = "";
	Enumeration penum = inputV.elements();
	while ( penum.hasMoreElements() ) {
	    temp = (String)penum.nextElement();
	    System.out.println(temp.trim());
	}
    }

    private void makeDirectory(String abs_dir) {
	File dir = new File (abs_dir);
	dir.mkdir();
	if(dir.isDirectory()) {
	    System.out.println("Directory:"+dir.getName()+" has been created!");
	}
	else {
	    System.out.println("Can not create "+dir.getName());
	}
    }

    private void connectToUrl (String urlAddress) {
        URL url = null;        

	try {            
	    url = new URL (urlAddress);
	    BufferedReader in = null;

	    try {                
		if (urlAddress.startsWith("https://")) {
		    HttpsURLConnection httpsConnection = null;
		    httpsConnection = (HttpsURLConnection)url.openConnection();                
		    httpsConnection.setDoOutput(false);                
		    httpsConnection.connect();                

		    System.out.println("Connection Code and Message:" + 
				   httpsConnection.getResponseCode() + 
				   " - " + httpsConnection.getResponseMessage()+"\n");
                
		    in = new BufferedReader(new InputStreamReader
			(httpsConnection.getInputStream()));
		}
		else if (urlAddress.startsWith("http://")) {
		    HttpURLConnection httpConnection = null;
		    httpConnection = (HttpURLConnection)url.openConnection();                
		    httpConnection.setDoOutput(false);                
		    httpConnection.connect();                

		    System.out.println("Connection Code and Message:" + 
				   httpConnection.getResponseCode() + 
				   " - " + httpConnection.getResponseMessage()+"\n");
                
		    in = new BufferedReader(new InputStreamReader
			(httpConnection.getInputStream()));
		}
		else {
		    System.out.println("Unknow protocal!");
		    System.exit(0);
		}

		StringBuffer buff1= new StringBuffer("");

		input = in.readLine();

		while (input != null) {
		    buff1.append(input);
		    input = in.readLine();
		}

		in.close();            

		input =  buff1.toString();
		input=input.replaceAll("\n","");
		input=input.replaceAll("\t","");
		input=input.replaceAll("&nbsp;","");
		input=input.replaceAll(">",">\n");
		input=input.replaceAll("</","\n</");

	    } catch (IOException ioe) {                
		System.out.println(ioe.getMessage());  
		System.exit(1);            
	    }        
	} catch (MalformedURLException mue) {            
	    System.out.println(mue.getMessage());            
	    System.exit(1);        
	} catch (Exception e) {            
	    e.printStackTrace();            
	    System.exit(1);        
	}    
    }
}






